<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/6/19
 * Time: 14:39
 */

namespace iBrand\Shop\Backend\Model;


class TakeStockPlan extends \iBrand\Shop\Core\Models\TakeStockPlan
{
    public function setConditionAttribute($value)
    {
        $this->attributes['condition'] = json_encode($value);
    }

    public function getConditionAttribute()
    {
        return json_decode($this->attributes['condition'], true);
    }

    public function getConditionTextAttribute()
    {
        $condition = $this->condition;
        $text = '';

        if ($condition) {
            foreach ($condition as $key => $value) {
                if ($key == 'year') {
                    $text .= '年份：' . $value . '；';
                } elseif ($key == 'season_id') {
                    switch ($value) {
                        case 1:
                            $name = '春';
                            break;
                        case 2:
                            $name = '夏';
                            break;
                        case 3:
                            $name = '秋';
                            break;
                        case 4:
                            $name = '冬';
                            break;
                    }
                    $text .= '季节：' . $name . '；';
                }
            }
        }
        return $text;
    }

    public function getStatusTextAttribute()
    {
        switch ($this->status) {
            case 0:
                return '盘点中';
                break;
            case 1:
                return '已盘点';
                break;
            case 2:
                return '已作废';
                break;
            default:
                return '未盘点';
        }
    }

    public function getCanCancelAttribute()
    {
        if ($this->items->count() OR $this->attributes['status'] == 2 OR $this->attributes['status'] == 1) {
            return false;
        }
        return true;
    }
}